package com.zxc.ompz;

import net.rim.device.api.io.URI;
import net.rim.device.api.system.Clipboard;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

public class URLScreen extends PopupScreen
					   implements FieldChangeListener{
	private String url;
	LabelField labelField;
	EditField editURL;
	private ButtonField buttonOK;
	private ButtonField buttonCancel;
	
	public URLScreen(){
		super(new VerticalFieldManager());
		this.url = Clipboard.getClipboard().toString();
		
		labelField = new LabelField("Input URL", Field.FIELD_HCENTER);
		editURL = new EditField("URL:", url);
		
		buttonOK = new ButtonField("OK", ButtonField.CONSUME_CLICK| Field.FIELD_HCENTER);
		buttonCancel = new ButtonField("Cancel", ButtonField.CONSUME_CLICK| Field.FIELD_HCENTER);
		buttonOK.setChangeListener(this);
		buttonCancel.setChangeListener(this);
        add(labelField);
        add(editURL);
		add(buttonOK);
		add(buttonCancel);
	}
	public void fieldChanged(Field field, int arg)	{
		if (field == buttonOK){
			url = editURL.getText().trim();
			boolean illegal = false;
			try{
				URI.create(url);
			}
			catch(Exception e){
				illegal = true;
			}
			if (illegal){
				editURL.setFocus();
			}
			else{
				OmpzApp.getTheScreen().addMedia(new Media(url));
				OmpzApp.getTheApp().popScreen(this);
			}
			return;
		}
		if (field == buttonCancel){
			OmpzApp.getTheApp().popScreen(this);
			return;
		}
	}
}
